package com.winit.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11Nio2Protocol;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;

import java.nio.charset.Charset;

public class TomcatConfiguration {
    @Bean
    public EmbeddedServletContainerCustomizer createEmbeddedServletContainerCustomizer() {
        return new MyEmbeddedServletContainerCustomizer();
    }

    class MyEmbeddedServletContainerCustomizer implements EmbeddedServletContainerCustomizer {
        @Override
        public void customize(ConfigurableEmbeddedServletContainer container) {
            TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory) container;
            tomcatFactory.setUriEncoding(Charset.forName("UTF-8"));
            tomcatFactory.addAdditionalTomcatConnectors(createNioConnector1());        //下面的操作可以参照上面的方法

        }

        private Connector createNioConnector1() {
            Connector connector = new Connector("org.apache.coyote.http11.Http11Nio2Protocol");
            Http11Nio2Protocol protocol = (Http11Nio2Protocol) connector.getProtocolHandler();
            protocol.setConnectionTimeout(200);
            protocol.setMaxThreads(200);
            protocol.setMaxConnections(1000);
            connector.setScheme("http");
            connector.setPort(9090);//自定义的
            connector.setRedirectPort(8443);
            return connector;
        }
    }
}

